﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HerGoS.Administration.Engine;
using HerGoS.Administration.Entities;
using HerGoS.ControlsUI;

namespace HerGoS.WebUI.Administracion.Areas
{
    public partial class Areas_ABM : PageBase 
    {
        
        #region Propiedades

        public Area Area
        {
            get
            {
                if (ViewState["Area"] != null)
                    return (Area)ViewState["Area"];
                return null;
            }
            set
            {
                ViewState["Area"] = value;
            }

        }

        #endregion


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                LoadCombos();
                if (Request.QueryString["id"] == null)
                {
                    Area = new Area();
                }
                else
                {
                    var Id = Convert.ToInt32(Request.QueryString["id"]);
                    Area = GestorArea.Obtener(Id);
                    this.txtCodigo.Text = Area.Codigo;
                    this.txtDescripcion.Text = Area.Descripcion;
                    
                    this.cboSuperior.SelectedValue = Area.IdAreaSuperior == 0 ? "0" : Area.IdAreaSuperior.ToString();
                }
                Initialize();
            }
        }
          void Initialize()
         {
            this.btnGuardar.Settings(new ButtonExtText());
            this.btnCerrar.Settings(new ButtonExtText());
           
            LoadAutoria(Area);
         } 
                

          private void LoadCombos()
        {
           this.cboSuperior.DataSource = GestorArea.Consultar();
           cboSuperior.DataTextField = "Descripcion";
            cboSuperior.DataValueField = "OID";
            cboSuperior.DataBind();
            cboSuperior.Items.Insert(0, new ListItem("--Ninguno-- ", "0"));
        }


          protected void btnGuardar_Click(object sender, EventArgs e)
          {
              Area.Codigo = this.txtCodigo.Text;
              Area.Descripcion = this.txtDescripcion.Text;
              Area.IdAreaSuperior = this.cboSuperior.SelectedValue.ToInteger() == 0  ? (int?) null : cboSuperior.SelectedValue.ToInteger();
              GestorArea.Guardar(Area);

              ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "closedialog", "parent.IntervaloCerrarPopup();", true);

          }


          protected void btnCerrar_Click(object sender, EventArgs e)
          {
              ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "closedialog", "parent.CerrarPopup();", true);
          }

    }
}